<template>
  <div class="home-container">
    <div class="main-content">
      <!-- 欢迎横幅 -->
      <div class="welcome-banner">
        <h1>欢迎来到 TimeShare</h1>
        <p>用时间交换技能，让知识在校园流动</p>
        <el-button type="primary" size="large" @click="router.push('/skill/publish')">
          <el-icon><Plus /></el-icon>
          发布技能
        </el-button>
      </div>

      <!-- 推荐技能 -->
      <div class="section">
        <h2 class="section-title">
          <el-icon><Star /></el-icon>
          个性化推荐
        </h2>
        <div class="skill-grid" v-loading="loading">
          <div v-for="skill in recommendedSkills" :key="skill.id" class="skill-card" @click="viewSkill(skill.id)">
            <div class="skill-cover">
              <img :src="skill.coverImage || '/default-skill.jpg'" alt="">
            </div>
            <div class="skill-info">
              <h3>{{ skill.title }}</h3>
              <p class="description">{{ skill.description }}</p>
              <div class="skill-meta">
                <el-tag size="small">{{ skill.categoryName }}</el-tag>
                <div class="stats">
                  <span><el-icon><View /></el-icon> {{ skill.viewCount }}</span>
                  <span><el-icon><Star /></el-icon> {{ skill.favoriteCount }}</span>
                </div>
              </div>
              <div class="user-info-small">
                <el-avatar :size="24" :src="skill.userAvatar">{{ skill.userNickname?.[0] }}</el-avatar>
                <span>{{ skill.userNickname }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 热门技能 -->
      <div class="section">
        <h2 class="section-title">
          <el-icon><TrendCharts /></el-icon>
          热门技能
        </h2>
        <div class="skill-grid" v-loading="hotLoading">
          <div v-for="skill in hotSkills" :key="skill.id" class="skill-card" @click="viewSkill(skill.id)">
            <div class="skill-cover">
              <img :src="skill.coverImage || '/default-skill.jpg'" alt="">
            </div>
            <div class="skill-info">
              <h3>{{ skill.title }}</h3>
              <p class="description">{{ skill.description }}</p>
              <div class="skill-meta">
                <el-tag size="small">{{ skill.categoryName }}</el-tag>
                <div class="stats">
                  <span><el-icon><View /></el-icon> {{ skill.viewCount }}</span>
                  <span><el-icon><Star /></el-icon> {{ skill.favoriteCount }}</span>
                </div>
              </div>
              <div class="user-info-small">
                <el-avatar :size="24" :src="skill.userAvatar">{{ skill.userNickname?.[0] }}</el-avatar>
                <span>{{ skill.userNickname }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { getRecommendedSkills, getHotSkills } from '@/api/recommend'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const hotLoading = ref(false)
const recommendedSkills = ref([])
const hotSkills = ref([])

const viewSkill = (id) => {
  router.push(`/skill/detail/${id}`)
}

const fetchRecommendedSkills = async () => {
  loading.value = true
  try {
    const res = await getRecommendedSkills({ limit: 8 })
    recommendedSkills.value = res.data || []
  } catch (error) {
    console.error('获取推荐技能失败：', error)
  } finally {
    loading.value = false
  }
}

const fetchHotSkills = async () => {
  hotLoading.value = true
  try {
    const res = await getHotSkills({ limit: 8 })
    hotSkills.value = res.data || []
  } catch (error) {
    console.error('获取热门技能失败：', error)
  } finally {
    hotLoading.value = false
  }
}

onMounted(async () => {
  // 只有在有 token 且没有用户信息时，才获取用户信息
  if (userStore.token && !userStore.userInfo) {
    await userStore.fetchUserInfo()
  }
  // 只有在已登录时才获取个性化推荐
  if (userStore.userInfo) {
    fetchRecommendedSkills()
  }
  // 热门技能无需登录即可查看
  fetchHotSkills()
})
</script>

<style scoped>
.home-container {
  min-height: 100%;
}

.main-content {
  padding: 30px 20px;
  animation: fadeIn 0.6s ease-out;
  max-width: 1400px;
  margin: 0 auto;
}

.welcome-banner {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 80px 40px;
  border-radius: 20px;
  text-align: center;
  margin-bottom: 40px;
  position: relative;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(102, 126, 234, 0.3);
  animation: fadeIn 0.8s ease-out;
}

.welcome-banner::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, transparent 70%);
  animation: pulse 4s ease-in-out infinite;
}

.welcome-banner h1 {
  font-size: 42px;
  font-weight: 800;
  margin-bottom: 16px;
  position: relative;
  z-index: 1;
  letter-spacing: -1px;
}

.welcome-banner p {
  font-size: 20px;
  margin-bottom: 36px;
  opacity: 0.95;
  position: relative;
  z-index: 1;
  font-weight: 300;
}

.welcome-banner .el-button {
  position: relative;
  z-index: 1;
  background: white;
  color: #667eea;
  font-size: 16px;
  font-weight: 600;
  padding: 16px 32px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.welcome-banner .el-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
  background: white;
  color: #764ba2;
}

.section {
  background: white;
  padding: 36px;
  border-radius: 16px;
  margin-bottom: 30px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
  animation: fadeIn 1s ease-out;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 28px;
  color: #333;
}

.section-title .el-icon {
  font-size: 28px;
  color: #667eea;
}

.skill-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
}

.skill-card {
  background: white;
  border: 1px solid #f0f2f5;
  border-radius: 16px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
}

.skill-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
  opacity: 0;
  transition: opacity 0.3s;
}

.skill-card:hover::before {
  opacity: 1;
}

.skill-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 16px 40px rgba(102, 126, 234, 0.2);
  border-color: #667eea;
}

.skill-cover {
  width: 100%;
  height: 180px;
  overflow: hidden;
  background: linear-gradient(135deg, #f5f7fa 0%, #e8ebf0 100%);
  position: relative;
}

.skill-cover::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(180deg, transparent 0%, rgba(0, 0, 0, 0.1) 100%);
}

.skill-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

.skill-card:hover .skill-cover img {
  transform: scale(1.1);
}

.skill-info {
  padding: 20px;
  position: relative;
  z-index: 1;
}

.skill-info h3 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #333;
  transition: color 0.3s;
}

.skill-card:hover .skill-info h3 {
  color: #667eea;
}

.description {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 16px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: 44px;
}

.skill-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f2f5;
}

.stats {
  display: flex;
  gap: 16px;
  font-size: 13px;
  color: #999;
  font-weight: 500;
}

.stats span {
  display: flex;
  align-items: center;
  gap: 4px;
  transition: color 0.3s;
}

.stats span:hover {
  color: #667eea;
}

.user-info-small {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: #666;
  font-weight: 500;
}

.user-info-small .el-avatar {
  border: 2px solid #f0f2f5;
  transition: all 0.3s;
}

.skill-card:hover .user-info-small .el-avatar {
  border-color: #667eea;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .main-content {
    padding: 20px 12px;
  }

  .welcome-banner {
    padding: 50px 24px;
  }

  .welcome-banner h1 {
    font-size: 32px;
  }

  .welcome-banner p {
    font-size: 16px;
  }

  .section {
    padding: 24px 16px;
  }

  .skill-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}
</style>

